//
//  HXFoodMadeVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/29.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXFoodMadeVC.h"
#import "HX_Macro.h"
#import "HXFoodMakeCell.h"
#import "HXFoodBigModel.h"
#import "HXCategorySmallModel.h"
#import "HXFoodSmallModel.h"
#import "UIView+BundleExtension.h"
#import "HXSelectCollectionCell.h"
#import "HXSelecollectionCell.h"
#import "HXss]Cell.h"
#import "HXReletedVideoCell.h"
#import "HXReletedBigModel.h"
#import "HXReletedSmallModel.h"

@interface HXFoodMadeVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *drawer;
@property (nonatomic, strong) UIButton *showBtn;
@property (nonatomic, strong) UIView *viewBottom;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger ID;
@property (nonatomic, strong) NSMutableArray *idArray;
@property (nonatomic, strong) NSMutableIndexSet* selectedIndexSet;
/**
 *  模型数组源
 */
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, assign) BOOL isPanduan;
@property (nonatomic, strong) HXFoodMakeCell *cell;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *seleImage;
@property (nonatomic, strong) UIView *seleView;
@property (nonatomic, strong) NSMutableArray *idArrays;
@property (nonatomic, assign) BOOL isBtn;
@property (nonatomic, strong) UIButton *isBtns;
@property (nonatomic, strong) UICollectionView *collectionViewItems;
@property (nonatomic, strong) UILabel *labelSele;
@property (nonatomic, strong) UICollectionView *seleCollectionView;
@property (nonatomic, strong) UICollectionView *sss;
@property (nonatomic, strong) NSString *seleString;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UICollectionView *relatedVideo;
@property (nonatomic, strong) NSMutableArray *arrId;
@property (nonatomic, strong) NSMutableArray *arrReletedVideo;
@property (nonatomic, strong) UIImageView *notFoundImg;
@property (nonatomic, assign) BOOL isReleted;
@end

@implementation HXFoodMadeVC
- (NSMutableArray *)arrReletedVideo {
    if (!_arrReletedVideo) {
        _arrReletedVideo = [NSMutableArray array];
    }
    return _arrReletedVideo;
}
- (NSMutableArray *)arrId {
    if (!_arrId) {
        _arrId = [NSMutableArray array];
    }
    return _arrId;
}
- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}
- (NSMutableArray *)idArrays {
    if (!_idArrays) {
        _idArrays = [NSMutableArray array];
    }
    return _idArrays;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    self.ID = 0;
    self.isPanduan = NO;
    self.title = @"食材搭配";
    self.idArray = [NSMutableArray array];
    [self getNetWorking];
    [self creatCollectionView];
    [self creatMaskView];
    [self drawShowView];
    [self creatTableView];
    [self creatShowBtn];
    [self creatBottomView];
    [self creatSeleCollectionView];
    [self creatReletedVideo];
    
}
- (void)creatReletedVideo {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    self.relatedVideo = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, SCREEN_HEIGHT - 70 - 64 - 80) collectionViewLayout:layout];
    self.relatedVideo.delegate = self;
    self.relatedVideo.dataSource = self;
    self.relatedVideo.backgroundColor = [UIColor whiteColor];
    [self.viewBottom addSubview:self.relatedVideo];
    [self.relatedVideo registerNib:[UINib nibWithNibName:@"HXReletedVideoCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HXReletedVideoCell"];
}
- (void)creatSeleCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 0);
    self.seleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 68) collectionViewLayout:layout];
    self.seleCollectionView.scrollEnabled = YES;
    self.seleCollectionView.backgroundColor = [UIColor whiteColor];
    self.seleCollectionView.delegate = self;
    self.seleCollectionView.dataSource = self;
    [self.viewBottom addSubview:self.seleCollectionView];
    self.seleCollectionView.hidden = YES;
    [self.seleCollectionView registerNib:[UINib nibWithNibName:@"HXSelecollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HXSelecollectionCell"];
}
- (void)getNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"token=88E92FD7679C76FA91A54FE14641AD60&methodName=MaterialSubtype&user_id=1666223&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
            HXFoodBigModel *bigModel = [HXFoodBigModel ModelWithDic:dic];
            [self.arrDataSource addObject:bigModel];
        }
        [_tableView reloadData];
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)getRelatedVideoNetWokingIdOne:(NSString *)IdOne IdTwo:(NSString *)IdTwo IdThree:(NSString *)IdThree {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"page=1&methodName=SearchMix&size=15&material_ids=%@%@%@&version=4.40&", IdOne, IdTwo, IdThree];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        HXReletedBigModel *bigModel = [HXReletedBigModel ModelWithDic:dicData];
        [self.arrReletedVideo addObject:bigModel];
        [_relatedVideo reloadData];
        if (bigModel.data.count == 0) {
            [self creatShowNotFoundView];
            self.notFoundImg.hidden = NO;
        } else {
            self.notFoundImg.hidden = YES;
        }
    } Failure:^(NSError *error) {
    }];
}
- (void)creatShowNotFoundView {
    self.notFoundImg = [[UIImageView alloc] init];
    self.notFoundImg.HX_width = 80;
    self.notFoundImg.HX_height = 130;
    self.notFoundImg.centerX = SCREEN_WIDTH / 2;
    self.notFoundImg.centerY = 250;
    self.notFoundImg.image = [UIImage imageNamed:@"noVideo"];
    [self.viewBottom addSubview:self.notFoundImg];
    self.notFoundImg.hidden = YES;
}
- (void)creatShowBtn {
    self.showBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _showBtn.frame = CGRectMake(_drawer.HX_x - 30, 0, 30, 70);
    _showBtn.centerY = self.collectionView.centerY;
    _showBtn.backgroundColor = [UIColor redColor];
    UIBezierPath *maskPathUser = [UIBezierPath bezierPathWithRoundedRect:self.showBtn.bounds byRoundingCorners:UIRectCornerTopLeft| UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayerUser = [[CAShapeLayer alloc] init];
    maskLayerUser.frame = self.showBtn.bounds;
    maskLayerUser.path = maskPathUser.CGPath;
    self.showBtn.layer.mask = maskLayerUser;
    [_showBtn setImage:[UIImage imageNamed:@"ArrowLeft"] forState:UIControlStateNormal];
    [_showBtn addTarget:self action:@selector(showTableViewWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_showBtn];
   
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _drawer.HX_width, _drawer.HX_height) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.drawer addSubview:_tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"tableView";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:ID];
    }
    HXFoodBigModel *bigModel = self.arrDataSource[indexPath.row];
    cell.textLabel.text = bigModel.text;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.ID = indexPath.row;
    [self dismissbackView];
    [_collectionView reloadData];
}
- (void)creatBottomView {
    self.viewBottom = [[UIView alloc] init];
    _viewBottom.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] init];
    label.text = @"选择2个或3个食材\n掌厨告诉你可以做哪些菜";
    _viewBottom.HX_x = 0;
    _viewBottom.HX_y = SCREEN_HEIGHT - 70 - 64;
    _viewBottom.HX_width = SCREEN_WIDTH;
    _viewBottom.HX_height = 70;
    label.HX_height = 70;
    label.HX_width = 160;
    label.textColor = [UIColor colorWithWhite:0.095 alpha:1.000];
    label.centerX = _viewBottom.centerX;
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    label.textAlignment = 1;
    self.labelSele = label;
    [self.view addSubview:_viewBottom];
    [self.viewBottom addSubview:label];
    self.lineView = [[UIView alloc] init];
    _lineView.HX_x = 0;
    _lineView.HX_y = 70 - 1;
    _lineView.HX_width = SCREEN_WIDTH;
    _lineView.HX_height = 1;
    self.lineView.backgroundColor = [UIColor colorWithWhite:0.707 alpha:1.000];
    [self.viewBottom addSubview:self.lineView];
}
- (void)creatMaskView {
    self.maskShowView = [[HXMaskShowView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.maskShowView.foodShowVC = self;
    self.maskShowView.alpha = 0.0;
    [self.view addSubview:_maskShowView];
}
- (void)drawShowView {
    self.drawer = [[UIView alloc] init];
    _drawer.backgroundColor = [UIColor whiteColor];
    _drawer.HX_height = SCREEN_HEIGHT - 64;
    _drawer.HX_width = SCREEN_WIDTH / 2;
    _drawer.HX_x = SCREEN_WIDTH;
    _drawer.HX_y = 0;
    [self.view addSubview:_drawer];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 60 - 70) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HXFoodMakeCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"Cell"];
}
- (void)dismissbackView {
    [self.showBtn bringSubviewToFront:self.maskShowView];
    [UIView animateWithDuration:0.5 animations:^{
        self.maskShowView.alpha = 0.0;
        self.drawer.HX_x = SCREEN_WIDTH;
        self.showBtn.HX_x = self.drawer.HX_x - 30;
        [self.showBtn setImage:[UIImage imageNamed:@"ArrowLeft"] forState:UIControlStateNormal];
        [self creatBottomViewHeight];
    }completion:^(BOOL finished) {
        self.isPanduan = NO;
    }];
}
- (void)showTableViewWithBtn:(UIButton *)btn {
    if (self.isPanduan == NO) {
        [UIView animateWithDuration:0.5 animations:^{
            self.drawer.HX_x = SCREEN_WIDTH / 2;
            self.showBtn.HX_x = self.drawer.HX_x - 30;
            self.maskShowView.alpha = 1;
            [self.showBtn setImage:[UIImage imageNamed:@"ArrowRight"] forState:UIControlStateNormal];
        }completion:^(BOOL finished) {
        }];
    } else {
        [self dismissbackView];
    }
    _isPanduan = !_isPanduan;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.collectionView) {
        if (self.arrDataSource.count) {
            HXFoodBigModel *bigModel = self.arrDataSource[self.ID];
            return bigModel.data.count;
        } else {
            return 0;
        }
    } else if (collectionView == self.seleCollectionView) {
        if (self.idArrays.count > 0) {
            return self.idArrays.count + 1;
        } else {
            return 0;
        }
    } else {
        HXReletedBigModel *bigModel = [self.arrReletedVideo firstObject];
        return bigModel.data.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectionView) {
        static NSString *identry = @"Cell";
        HXFoodMakeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identry forIndexPath:indexPath];
        if (!cell) {
            NSLog(@"没有生成");
        }
        if (self.arrDataSource.count) {
            HXFoodBigModel *bigModel = self.arrDataSource[self.ID];
            cell.smallModel = bigModel.data[indexPath.row];
            cell.backgroundColor = [UIColor whiteColor];
            [cell sizeToFit];
            if ([self.idArrays containsObject:cell.smallModel.text]) {
                cell.imgBtn.selected = YES;
            } else {
                cell.imgBtn.selected = NO;
            }
//            [cell.imgBtn addTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
        }
        return cell;
    } else if (collectionView == self.seleCollectionView){
        HXSelecollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HXSelecollectionCell" forIndexPath:indexPath];
        NSArray *arr = [[self.idArrays reverseObjectEnumerator] allObjects];
        if (indexPath.item < arr.count) {
            cell.labelName.text = arr[arr.count - 1 - indexPath.row];
        } else {
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@""];
            NSTextAttachment *attach = [[NSTextAttachment alloc] init];
            attach.image = [UIImage imageNamed:@"material_add"];
            attach.bounds = CGRectMake(0, 0, 15, 15);
            NSAttributedString *attStr  = [NSAttributedString attributedStringWithAttachment:attach];
            [string appendAttributedString:attStr];
            cell.labelName.attributedText = string;
            cell.labelName.backgroundColor = [UIColor whiteColor];
            cell.deleImg.image = [UIImage imageNamed:@""];
            if (self.idArrays.count == 3) {
                cell.labelName.hidden = YES;
            }
        }
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else {
        HXReletedVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HXReletedVideoCell" forIndexPath:indexPath];
        HXReletedBigModel *bigModel = [self.arrReletedVideo firstObject];
        HXReletedSmallModel *smallModel = bigModel.data[indexPath.row];
        [cell.imgHeader sd_setImageWithURL:[NSURL URLWithString:smallModel.image]];
        cell.labelName.text = smallModel.title;
        return cell;
    }
}
//- (void)chooseImage:(UIButton *)btn
//{
//    btn.selected = !btn.selected;
//}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.arrReletedVideo removeAllObjects];
    HXFoodMakeCell *cell = (HXFoodMakeCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (collectionView == self.collectionView) {
        HXFoodBigModel *bigModel = self.arrDataSource[self.ID];
        cell.smallModel = bigModel.data[indexPath.row];
        cell.imgBtn.selected = !cell.imgBtn.selected;
        if (self.idArrays.count <= 2) {
            if (cell.imgBtn.selected == YES) {
                [self.idArrays addObject:cell.smallModel.text];
                [self.seleCollectionView reloadData];
                [self.arrId addObject:cell.smallModel.ID];
                if (self.arrId.count == 2) {
                    [self getRelatedVideoNetWokingIdOne:[NSString stringWithFormat:@"%@%%2C", self.arrId[0]] IdTwo:[NSString stringWithFormat:@"%@%%2C", self.arrId[1]] IdThree:@""];
                }
                if (self.arrId.count == 3) {
                    [self getRelatedVideoNetWokingIdOne:[NSString stringWithFormat:@"%@%%2C", self.arrId[0]] IdTwo:[NSString stringWithFormat:@"%@%%2C", self.arrId[1]] IdThree:[NSString stringWithFormat:@"%@%%2C", self.arrId[2]]];
                }
            } else {
                [self.idArrays removeObject:cell.smallModel.text];
                [self.arrId removeObject:cell.smallModel.ID];
                if (self.arrId.count >= 2) {
                    [self getRelatedVideoNetWokingIdOne:[NSString stringWithFormat:@"%@%%2C", self.arrId[0]] IdTwo:[NSString stringWithFormat:@"%@%%2C", self.arrId[1]] IdThree:@""];
                }
                [self.seleCollectionView reloadData];
            }
            if (self.idArrays.count > 1) {
                [self showBottomView];
            } else {
                [self creatBottomViewHeight];
            }
        } else {
            [self.idArrays removeObject:cell.smallModel.text];
            cell.imgBtn.selected = NO;
            [self.seleCollectionView reloadData];
        }
    } else {
        if (self.idArrays.count <= 2) {
            [self dismissbackView];
            [self creatBottomViewHeight];
        }
        if (indexPath.row < self.idArrays.count) {
            [self.idArrays removeObjectAtIndex:indexPath.row];
            [self.arrId removeObjectAtIndex:indexPath.row];
            if (self.arrId.count >= 2) {
                [self getRelatedVideoNetWokingIdOne:[NSString stringWithFormat:@"%@%%2C", self.arrId[0]] IdTwo:[NSString stringWithFormat:@"%@%%2C", self.arrId[1]] IdThree:@""];
            }
            [self.seleCollectionView reloadData];
            [self.collectionView reloadData];
        }
    }
    if (self.idArrays.count > 0) {
        self.seleCollectionView.hidden = NO;
    } else {
        self.seleCollectionView.hidden = YES;
    }
}
- (void)creatBottomViewHeight {
    [UIView animateWithDuration:0.3 animations:^{
        self.viewBottom.HX_height = 70;
        self.viewBottom.HX_y = SCREEN_HEIGHT - 70 - 64;
    }completion:^(BOOL finished) {
        
    }];
}
- (void)showBottomView {
    [UIView animateWithDuration:0.3 animations:^{
        _viewBottom.HX_y = SCREEN_HEIGHT - 450 - 64;
        self.viewBottom.HX_height = 450;
        self.maskShowView.alpha = 1;
    }completion:^(BOOL finished) {
        
    }];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView == self.relatedVideo) {
        return 5;
    } else {
        return 10;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectionView) {
        return CGSizeMake((self.collectionView.HX_width - 70) / 4, 100);
    } else if (collectionView == self.seleCollectionView) {
        return CGSizeMake(60, 50);
    } else {
        return CGSizeMake(SCREEN_WIDTH / 3.2, 150);
    }
}
@end